import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { AIManager } from "../scene/ai/AIManager";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
import { AIType } from "../scene/ai/AIType";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { Game } from "../Game";
import { RoleDrugEnoughType } from "../view/avatar/RoleDrugEnoughType";
import { IGuideDrugModule } from "../../../modules/interfaces/IGuideDrugModule";
import { SceneConfig } from "../resource/SceneConfig";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { EntityType } from "../rules/EntityType";
import { IView } from "../../mvc/interfaces/IView";
import { GuideDrugModule } from "../../../modules/GuideDrugModule";
type int = number;
//class GuideDrugController
    
    export  class GuideDrugController extends Controller
    {
       

        /* internal  */onGotoBuyDrug(arg1: DataEvent): void
        {
            AIManager.onAutoPathAIControl(arg1.data as TaskTargetInfo);
            return;
        }

        /* internal  */onFlyToBuyDrug(arg1: DataEvent): void
        {
            AIManager.onAIControl(arg1.data, AIType.AI_Convey);
            return;
        }

        /* internal  */onGuideDrugBuyClose(arg1: DataEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuideDrugBuyClose));
            return;
        }

        /* internal  */onRoleDrugNotEnough(arg1: DataEvent): void
        {
            this._drugType = int(arg1.data);
            if (!Game.isSceneInit()) 
            {
                this._sceneNotInit = true;
                Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
                return;
            }
            this.guideDrug(this._drugType);
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            if (this._sceneNotInit) 
            {
                this.guideDrug(this._drugType);
                this._sceneNotInit = false;
            }
            return;
        }

        /* internal  */guideDrug(arg1: int): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=null;
            if (cache.vip.isVIP) 
            {
                if (arg1 != RoleDrugEnoughType.allEnough) 
                {
                    if (cache.shop.isVipShopOpening) 
                    {
                        return;
                    }
                    if (view.isHide) 
                    {
                        (view as IGuideDrugModule).updateData(null, arg1, true);
                        view.show();
                    }
                }
                else if (_view && !_view.isHide) 
                {
                    _view.hide();
                }
                return;
            }
            if (arg1 != RoleDrugEnoughType.allEnough) 
            {
                if (view.isHide) 
                {
                    loc1 = cache.scene.getDragNPC();
                    if (loc1 != -1) 
                    {
                        loc2 = SceneConfig.instance.getSceneInfoByNpcId(loc1);
                        if (loc2 == null) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(20330));
                        }
                        else 
                        {
                            loc3 = loc2.getNpcInfo(loc1);
                            if (!this.drugTarget) 
                            {
                                this.drugTarget = new TaskTargetInfo();
                            }
                            this.drugTarget.id = loc1;
                            this.drugTarget.mapId = loc2.sMapDefine.mapId;
                            this.drugTarget.x = loc3.snpc.point.x;
                            this.drugTarget.y = loc3.snpc.point.y;
                            this.drugTarget.name = loc3.tnpc.name;
                            this.drugTarget.mapName = loc2.sMapDefine.name;
                            this.drugTarget.targetType = EntityType.NPC;
                            (view as IGuideDrugModule).updateData(this.drugTarget, arg1);
                            view.show();
                        }
                    }
                }
            }
            else if (_view && !_view.isHide) 
            {
                _view.hide();
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.RoleDrugNotEnough, this.onRoleDrugNotEnough);
            return;
        }

        protected /* override */ initView(): IView
        {
            var loc1=new GuideDrugModule();
            loc1.addEventListener(EventName.AutoPathBuyDrug, this.onGotoBuyDrug);
            loc1.addEventListener(EventName.FlyToBuyDrug, this.onFlyToBuyDrug);
            loc1.addEventListener(EventName.GuideDrugBuyClose, this.onGuideDrugBuyClose);
            return loc1;
        }

        private /* var */_sceneNotInit: Boolean;

        private /* var */_drugType: int;

        private /* var */drugTarget: TaskTargetInfo;
    }
